(*
  You can use/redistribute/change this file as long as you are using it under
  GPL terms. See www.gnu.org for more information about this license.

  Author        : Popov John
  Updates       : Ciobanu Alexander
  Last Updated  : 03.July.2004
*)

{ Include Compile Directives ! }
{$I Utils\directives.inc}
program EdukaPlus;

{$R *.res}

{%File '3rdParty\SynEdit\SynEdit.inc'}

uses
  Forms,
  Menus,
  SysUtils,
  Windows,
  Messages,
  Classes,
  Debug in 'Utils\Debug.pas',
  DebugMain in 'Dialogs\DebugMain.pas' {DebugForm},
  Main in 'Dialogs\Main.pas' {MainEdukaForm},
  Select_Interpreter in 'Dialogs\Select_Interpreter.pas' {Dialog_Select_Interpreter},
  Select_Language in 'Dialogs\Select_Language.pas' {Dialog_Select_Language},
  Translate in 'Utils\Translate.pas',
  DesignInside in 'Utils\DesignInside.pas',
  About in 'Dialogs\About.pas' {AboutForm},
  Select_Condition in 'Dialogs\Select_Condition.pas' {Dialog_Select_Condition},
  Select_Times in 'Dialogs\Select_Times.pas' {Dialog_Select_Times},
  Select_Name in 'Dialogs\Select_Name.pas' {Dialog_Select_Name},
  _Logics_ in 'Logics\_Logics_.pas',
  Lists in 'Logics\Lists.pas',
  Movement in 'Utils\Movement.pas',
  ProTypes in 'Utils\ProTypes.pas',
  ProSyntax in 'Logics\ProSyntax.pas',
  EditorFuncs in 'Editor\EditorFuncs.pas',
  Files in 'Editor\Files.pas',
  Version in 'Utils\Version.pas',
  Utils in 'Utils\Utils.pas',
  Errors in 'Logics\Errors.pas',
  Check_Save in 'Dialogs\Check_Save.pas' {Dialog_Check_Save},
  SynProSyn in 'Editor\SynProSyn.pas',
  PrnExpt in 'Editor\PrnExpt.pas',
  Resources in 'Utils\Resources.pas',
  Select_Student in 'Dialogs\Select_Student.pas' {Dialog_Select_Student},
  PackageManager in 'Dialogs\PackageManager.pas' {PackageForm},
  ContextHelp in 'Dialogs\ContextHelp.pas' {Context_Form},
  Login in 'Dialogs\Login.pas' {LoginForm},
  SynEdukaExportHTML in 'Editor\SynEdukaExportHTML.pas',
  SHDocVw_TLB in '3rdParty\Microsoft\SHDocVw_TLB.pas',
  SynCompletionProposal in '3rdParty\SynEdit\SynCompletionProposal.pas',
  SynEdit in '3rdParty\SynEdit\SynEdit.pas',
  SynEditExport in '3rdParty\SynEdit\SynEditExport.pas',
  SynEditHighlighter in '3rdParty\SynEdit\SynEditHighlighter.pas',
  SynEditKbdHandler in '3rdParty\SynEdit\SynEditKbdHandler.pas',
  SynEditKeyCmds in '3rdParty\SynEdit\SynEditKeyCmds.pas',
  SynEditKeyConst in '3rdParty\SynEdit\SynEditKeyConst.pas',
  SynEditMiscClasses in '3rdParty\SynEdit\SynEditMiscClasses.pas',
  SynEditMiscProcs in '3rdParty\SynEdit\SynEditMiscProcs.pas',
  SynEditPrint in '3rdParty\SynEdit\SynEditPrint.pas',
  SynEditPrinterInfo in '3rdParty\SynEdit\SynEditPrinterInfo.pas',
  SynEditPrintHeaderFooter in '3rdParty\SynEdit\SynEditPrintHeaderFooter.pas',
  SynEditPrintMargins in '3rdParty\SynEdit\SynEditPrintMargins.pas',
  SynEditPrintTypes in '3rdParty\SynEdit\SynEditPrintTypes.pas',
  SynEditSearch in '3rdParty\SynEdit\SynEditSearch.pas',
  SynEditStrConst in '3rdParty\SynEdit\SynEditStrConst.pas',
  SynEditTextBuffer in '3rdParty\SynEdit\SynEditTextBuffer.pas',
  SynEditTypes in '3rdParty\SynEdit\SynEditTypes.pas',
  SynExportHTML in '3rdParty\SynEdit\SynExportHTML.pas',
  SynExportRTF in '3rdParty\SynEdit\SynExportRTF.pas',
  SynHighlighterGeneral in '3rdParty\SynEdit\SynHighlighterGeneral.pas',
  SynTextDrawer in '3rdParty\SynEdit\SynTextDrawer.pas',
  Spin in '3rdParty\CodeGear\Spin.pas';

{$IFDEF _DEBUG_}
Const
  DbgUName = './EdukaPlus.pas';
{$ENDIF}


Var
 WasFP   : Integer;
 MyMutex : THandle;
 Spast   : String;
 I       : Integer;
 CIPrt   : Integer;
  CITsk,
   CITst,
    CIInt,
     CIPkg  : Boolean;

  IcH : HICON;

begin
 // Hack to prevent crash on multi-core machines (Use just the firt core!
 SetProcessAffinityMask(GetCurrentProcess(), $00000001);

 {$IFDEF _DEBUG_}
 ReportMemoryLeaksOnShutdown := true;
 {$ENDIF}

 MyMutex := 0;
 ExecutionMode := eemPreRun;

 Application.Initialize;
 Application.Title := 'Eduka+';
 Application.OnException := MainEdukaForm.ExceptionHandler;

try
{$IFDEF _DEBUG_}
  DbgPrint(DbgUname+' : Main(Param0:'+DbgStr(ParamStr(0))+'; Param1:'+DbgStr(ParamStr(1))+'; Param2:'+DbgStr(ParamStr(2))+')');
  DbgPrint(DbgUname+' : Main() | Started Application !');
  DbgPlus;
{$ENDIF}

 {-- Must be initiated before using forms -------}
 ExtendedPackage  := false;
 ExtendedDynamic  := false;
 ExtendedDebug    := false;
 ExtendedAcademic := false;

 {--Find Out caps of this PC--}
 {
 if Screen.Width < 1024 then
    DesignInside.MenusStyle := msOffice else
    DesignInside.MenusStyle := msXP;
 }
 AdministrationPassword := EdukaDefaultPassword;

 Spast := LoadOption('ExtendedOpts' ,VKEY,'' ,RKEY);
 if Spast <> '' then
  begin
    Spast := ByteRepToString ( Spast );
    Spast := BinaryCodeToString( Spast );

    if Length( Spast ) >= 4 then
    begin
     ExtendedPackage  := ( Spast[1] = '1' );
     ExtendedDynamic  := ( Spast[2] = '1' );
     ExtendedDebug    := ( Spast[3] = '1' );
     ExtendedAcademic := ( Spast[4] = '1' );

     Delete( Spast, 1, 4 );
     AdministrationPassword := Spast;
    end;

  end;

  OSExceptionHandler( Application );
 {------------------------------------------------}

  CITsk := False;
  CITst := False;
  CIInt := False;
  CIPkg := False;

  SPast := '';
  WasFP := -1;
  CIPrt := -1;

  if UpperCase(Paramstr(1))='--ADMIN' then
     AdministrativeStart  := true else AdministrativeStart := false;

   if UpperCase(Paramstr(1))='--ANT' then
   begin
    Inc(WasFP);
    CIPrt := Ord(piAnt);
    CIInt := True;
   end;

   if (UpperCase(Paramstr(1))='--ETK') and (ExtendedAcademic) then
   begin
    inc(WasFP);
    CITsk := True;
   end;

  if UpperCase(Paramstr(1))='--KNG' then
   begin
    Inc(WasFP);
    CIPrt := Ord(piKng);
    CIInt := True;
   end;

   if UpperCase(Paramstr(1))='--TUX' then
   begin
    Inc(WasFP);
    CIPrt := Ord(piTux);
    CIInt := True;
   end;

   if (UpperCase(Paramstr(1))='--EPK') and (ExtendedPackage) then
   begin
    inc(WasFP);
    CIPkg := True;
   end;

   if (UpperCase(Paramstr(1))='--ETT') and (ExtendedAcademic) then
   begin
    inc(WasFP);
    CITst := True;
   end;
  
   if WasFP = 0 then
   begin
     if ParamStr(2)<>'' then
       Spast := ParamStr(2);

   end else
   begin
     if ParamStr(1)<>'' then
          Spast := ParamStr(1);
   end;

    if (Spast <> '') and (not CITsk) and (not CITst) and (not CIInt) then
    begin
     if AnsiLowerCase(ExtractFileExt(Spast)) = extFileKng then
      begin
        CIPrt := Ord(piKng);
      end else
     if AnsiLowerCase(ExtractFileExt(Spast)) = extFileTux then
      begin
        CIPrt := Ord(piTux);
      end else
     if AnsiLowerCase(ExtractFileExt(Spast)) = extFileAnt then
      begin
        CIPrt := Ord(piAnt);
      end else
     if AnsiLowerCase(ExtractFileExt(Spast)) = extFileExercise then
      begin
        CITsk := True;
      end else
     if AnsiLowerCase(ExtractFileExt(Spast)) = extFilePackage then
      begin
        CIPkg := True;
      end else
     if AnsiLowerCase(ExtractFileExt(Spast)) = extFileTest then
      begin
        CITst := True;
      end else Spast := '';

    end;

    if CITsk then  CIPrt := 1000 else
    if CITst then  CIPrt := 1001;
    if CIPkg then  CIPrt := 1002;

    if OpenMutex(MUTEX_ALL_ACCESS , False, 'EdukaMutexInstallation') <> 0 then
    begin
      {$IFDEF _DEBUG_}
        DbgMinus;
        DbgPrint(DbgUname+' : Main() | Application Terminated !');
      {$ENDIF}
      
      if CIPrt <> -1 then
         SendMessage(FindWindow('TMainEdukaForm',nil),WM_GetThoseFiles, 1, CIPrt);

      if Spast <> '' then
       begin
        for I:=1 to Length(Spast) do
         SendMessage(FindWindow('TMainEdukaForm',nil),WM_GetThoseFiles, Integer(Spast[I]), 0);
       end;

       if (CIPrt <> -1) or (Spast <> '') then SendMessage(FindWindow('TMainEdukaForm',nil),WM_GetThoseFiles, 0, 0);

       Exit;
    end;

   
   MyMutex := CreateMutex(nil, False, 'EdukaMutexInstallation');

   ActiveStaticColor       := ppActiveStaticColor;
   ActiveStaticFontColor   := ppActiveStaticFontColor;

   DefaultStaticColor      := ppDefaultStaticColor;
   DefaultStaticFontColor  := ppDefaultStaticFontColor;
   DefaultStaticMoveFontColor := ppDefaultStaticMoveFontColor;
   
   LettersColor            := ppLettersColor;

  if Screen.Fonts.IndexOf(DefaultFont)=-1 then
     MainUsedFont := AddFont else MainUsedFont:=DefaultFont;

  OSExceptionHandler( Application );

  Application.CreateForm(TMainEdukaForm, MainEdukaForm);
  Application.CreateForm(TDialog_Select_Interpreter, Dialog_Select_Interpreter);
  Application.CreateForm(TDialog_Select_Language, Dialog_Select_Language);
  Application.CreateForm(TAboutForm, AboutForm);
  Application.CreateForm(TDialog_Select_Condition, Dialog_Select_Condition);
  Application.CreateForm(TDialog_Select_Times, Dialog_Select_Times);
  Application.CreateForm(TDialog_Select_Name, Dialog_Select_Name);
  Application.CreateForm(TDebugForm, DebugForm);
  Application.CreateForm(TDialog_Check_Save, Dialog_Check_Save);
  Application.CreateForm(TDialog_Select_Student, Dialog_Select_Student);
  Application.CreateForm(TPackageForm, PackageForm);
  Application.CreateForm(TContext_Form, Context_Form);
  {<==================================================>}
  IcH := LoadIcon(GetWindowLong(Context_Form.Handle,GWL_HINSTANCE),'IDI_ICON21');
  if IcH<>0 then Context_Form.Icon.Handle := IcH;
{<==================================================>}

  Application.CreateForm(TLoginForm, LoginForm);


  if CIPrt <> -1 then
    SendMessage(FindWindow('TMainEdukaForm',nil),WM_GetThoseFiles, 1, CIPrt);

  if Spast <> '' then
   begin
    for I:=1 to Length(Spast) do
     SendMessage(FindWindow('TMainEdukaForm',nil),WM_GetThoseFiles, Integer(Spast[I]), 0);
   end;

  if (CIPrt <> -1) or (Spast <> '') then
      SendMessage(FindWindow('TMainEdukaForm',nil),WM_GetThoseFiles, 0, 0);

{$WARNINGS OFF}
 FileSndError        := LoadFileIntoArray(ExtractFilePath(ParamStr(0))+'Sounds\snderror.wav');
 FileSndSuccess      := LoadFileIntoArray(ExtractFilePath(ParamStr(0))+'Sounds\sndsuccess.wav');
 FileSndRuntimeError := LoadFileIntoArray(ExtractFilePath(ParamStr(0))+'Sounds\sndrerror.wav');
 FileSndInformation  := LoadFileIntoArray(ExtractFilePath(ParamStr(0))+'Sounds\sndinfo.wav');
 FileSndOverTick     := LoadFileIntoArray(ExtractFilePath(ParamStr(0))+'Sounds\sndover.wav');
 FileSndDlgTick      := LoadFileIntoArray(ExtractFilePath(ParamStr(0))+'Sounds\snddlg.wav');
 FileSndJustTick     := LoadFileIntoArray(ExtractFilePath(ParamStr(0))+'Sounds\sndtick.wav');

 MainEdukaForm.Options_Sound.Visible := MainEdukaForm.Options_Sound.Visible and
  ( ( FileSndError        <> nil ) or
    ( FileSndSuccess      <> nil ) or
    ( FileSndRuntimeError <> nil ) or
    ( FileSndInformation  <> nil ) or
    ( FileSndOverTick     <> nil ) or
    ( FileSndDlgTick      <> nil ) or
    ( FileSndJustTick     <> nil ) );
{$WARNINGS ON}

 SoundStatus(True);
 
 OSExceptionHandler( Application );
except
 on E: Exception do ExceptionHandler( Application, E );
end;

 ExecutionMode := eemRun;

 Application.Run;

 ExecutionMode := eemTerminate;
 
try
  ReleaseMutex(MyMutex);
except
 on E: Exception do ExceptionHandler( Application, E );
end;

try
  RaiseLastOSError();
except
 on E: Exception do ExceptionHandler( Application, E );
end;

 if icH <> 0 then
    DestroyIcon(IcH);

{$IFDEF _DEBUG_}
  DbgMinus;
  DbgPrint(DbgUname+' : Main() | Application Terminated !');
{$ENDIF}
end.
